import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
import 'package:flutterdemo/0demo/common/common.dart';
import 'package:flutterdemo/0demo/native/test_platform_view_channel.dart';

class NativeViewPage extends StatefulWidget {
  @override
  _NativeViewPageState createState() => _NativeViewPageState();
}

class _NativeViewPageState extends State<NativeViewPage> {
  @override
  void initState() {
    _initNativePlugin();
    _setStatusBarBg();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    setStatusBarBlack();
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: dark1,
      child: Scaffold(
        body: Container(
          child: Platform.isAndroid
              ? AndroidView(
                  viewType: "TestView", //对应原生端唯一表示字符串
                  creationParams: <String, String>{"url": "http://www.baidu.com"}, //向原生传递参数
                  creationParamsCodec: const StandardMessageCodec(), //参数的编码，如果有参数，这个必须要
                  gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
                    new Factory<OneSequenceGestureRecognizer>(
                      () => new EagerGestureRecognizer(),
                    ),
                  ].toSet(), //手势处理
                )
              : Container(),
        ),
      ),
    );
  }

  void _initNativePlugin() {
    WidgetsBinding.instance.addPostFrameCallback((callback) {
      setStatusBarBlack();

      TestPlatformViewChannel.getInstance().eventCanClick = (channelEvent) {
        print("channelEvent == $channelEvent");
      };
    });
  }

  Future<void> _setStatusBarBg() async {
    await FlutterStatusbarcolor.setNavigationBarColor(Colors.orange[200]);
  }
}
